# -*- coding: utf-8 -*-
"""
ECharts 折线图组件
"""
from pyecharts.charts import Line
from pyecharts import options as opts
from .xy_chart_base import TgXYChartBase
import pandas as pd


class TgLineChart(TgXYChartBase):
    """ECharts 折线图组件"""

    def __init__(self, parent=None):
        super().__init__(parent)
        
        # 折线图特有配置
        self._is_smooth = False
        self._show_symbol = True
        self._symbol_size = 6
        self._line_width = 2
        self._area_style = False
        self._connect_nulls = True

    def set_smooth(self, smooth: bool = True):
        """设置是否平滑曲线"""
        self._is_smooth = smooth
        self.update_chart()
        return self

    def set_symbol(self, show: bool = True, size: int = 6):
        """设置数据点符号"""
        self._show_symbol = show
        self._symbol_size = size
        self.update_chart()
        return self

    def set_line_style(self, width: int = 2, area_style: bool = False):
        """设置线条样式"""
        self._line_width = width
        self._area_style = area_style
        self.update_chart()
        return self

    def set_connect_nulls(self, connect: bool = True):
        """设置是否连接空值"""
        self._connect_nulls = connect
        self.update_chart()
        return self

    def create_chart(self) -> Line:
        """创建图表实例"""
        return Line()

    def update_chart(self):
        """
        更新图表
        """
        if self.df is None or self.x_axis_column is None or not self.y_axis_columns:
            return

        chart = self.create_chart()
        chart.add_xaxis(self.df[self.x_axis_column].tolist())

        for col in self.y_axis_columns:
            chart.add_yaxis(
                series_name=col,
                y_axis=self.df[col].tolist(),
                is_smooth=self._is_smooth,
                symbol="circle",
                symbol_size=self._symbol_size if self._show_symbol else 0,
                linestyle_opts=opts.LineStyleOpts(width=self._line_width),
                areastyle_opts=opts.AreaStyleOpts(opacity=0.5) if self._area_style else None,
                is_connect_nones=self._connect_nulls
            )
        
        self.render_chart(chart)